//
//  CustomTabBar.m
//  GetFit3.0
//
//  Created by 秀才 on 2016/11/26.
//  Copyright © 2016年 lxc. All rights reserved.
//

#import "CustomTabBar.h"
#import "SNNavigationViewController.h"
#import "SNHomePageViewController.h"
#import "SNTrackViewController.h"
#import "SNEquipmentViewController.h"
#import "SNSettingViewController.h"
#import "FindPhoneView.h"
#import <UserNotifications/UserNotifications.h>
@interface CustomTabBar ()
{
    UIView *_tabBarView;
}
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) NSArray *normalImageArray;
@property (nonatomic, strong) NSArray *selectImageArray;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSMutableArray *vcs;

@end

@implementation CustomTabBar
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [SN_PublicClass setBLTControlTDelegate:self];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
     [SN_PublicClass setBLTControlTDelegate:nil];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    //  隐藏UITabBarButton
    self.tabBar.hidden = YES;
    
    _normalImageArray = [NSArray arrayWithObjects:@"主页1",@"跑道1",@"设备1",@"设置1", nil];
    
    _selectImageArray = [NSArray arrayWithObjects:@"主页",@"跑道",@"设备",@"设置", nil];
    
    _titleArray = [NSArray arrayWithObjects:SN_Text(@"日常"),SN_Text(@"活动"),SN_Text(@"设备"),SN_Text(@"用户"), nil];
    
    SNHomePageViewController *Vc1 = [[SNHomePageViewController alloc]init];
    SNNavigationViewController *nav1 = [[SNNavigationViewController alloc]initWithRootViewController:Vc1];
    
    SNTrackViewController *Vc2 = [[SNTrackViewController alloc]init];
    SNNavigationViewController *nav2 = [[SNNavigationViewController alloc]initWithRootViewController:Vc2];
    
    SNEquipmentViewController *Vc3 = [[SNEquipmentViewController alloc] init];
    SNNavigationViewController *nav3 = [[SNNavigationViewController alloc]initWithRootViewController:Vc3];
    
    SNSettingViewController *Vc4 = [[SNSettingViewController alloc]init];
    SNNavigationViewController *nav4 = [[SNNavigationViewController alloc]initWithRootViewController:Vc4];
    
    _vcs = @[nav1,nav2,nav3,nav4];
    [self setViewControllers:_vcs animated:YES];
    
    _tabBarView = [[UIView alloc]initWithFrame:CGRectMake(0, HHHHEIGHT - 49, HHHWIDTH, 49)];
    _tabBarView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tabBarView];
    
//    UIView *line = [[UIView alloc] init];
//    line.frame = CGRectMake(0, 0, HHHWIDTH, 3.0);
//    line.backgroundColor = [UIColor colorFromHex:0xf3f3f3];
//    [_tabBarView addSubview:line];

    SNShadowView *shadowLine = [[SNShadowView alloc] init];
    shadowLine.frame = CGRectMake(0, 0, HHHWIDTH, 3.0);
    [shadowLine showWithType:1];
    [_tabBarView addSubview:shadowLine];
    
    _tabBarView.backgroundColor = BBIGLIGHTCOLOR;
    
    for (int i = 0; i < _vcs.count; i ++)
    {
        UIButton *tarBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tarBarButton.frame = CGRectMake(HHHWIDTH/_vcs.count *i, 1, HHHWIDTH/_vcs.count, 49 + FitSimpleIpx(0, 12));
        tarBarButton.tag = i;
        [tarBarButton addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_tabBarView addSubview:tarBarButton];
        
        UIImageView *imageV =  [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 28 +FitSimpleIpx(0, 5), 28 +FitSimpleIpx(0, 5))];
        imageV.tag = i + 1000;
        imageV.center = CGPointMake(tarBarButton.x + HHHWIDTH/_vcs.count /2, 47/2 - FitSimpleIpx(0, 2) - 6 );
        imageV.image = [UIImage image:_normalImageArray[i]];
        [_tabBarView addSubview:imageV];
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,HHHWIDTH/_vcs.count, 20)];
        titleLabel.tag = i + 100;
        titleLabel.font = SNFONTMIN(12.0);
        titleLabel.textColor = [UIColor lightGrayColor];
        titleLabel.center = CGPointMake(tarBarButton.center.x ,tarBarButton.center.y + 13 - FitSimpleIpx(0, 6));
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = _titleArray[i];
        titleLabel.textColor = SNCOLORGRAY;
        [_tabBarView addSubview:titleLabel];
        
        if (i == 0)
        {
            titleLabel.textColor = SNCOLORBLACK;
            imageV.image = [UIImage image:_selectImageArray[i]];
        }
    }
    
    _lineView = [[UIView alloc] init];
    _lineView.frame = CGRectMake(0, 47 + FitSimpleIpx(0, 12), HHHWIDTH/_vcs.count, 2);
    _lineView.backgroundColor = [UIColor colorFromHex:0x6d6d6d];
    [_tabBarView addSubview:_lineView];
    KK_ShareData.currentVCTag = 0;
}

- (void)dealloc
{
    
}

- (void)senderClick:(UIButton *)sender
{
    [UIView animateWithDuration:VIEWANIMATETIME animations:^{
        sender.backgroundColor = [UIColor clearColor];
        sender.alpha = 1.0;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)tabBarButtonClick:(UIButton *)sender
{
    KK_ShareData.lastVcTag = KK_ShareData.currentVCTag;
    KK_ShareData.currentVCTag = sender.tag;
//    NSLog(@"从>>>%d 到>>>%d",KK_ShareData.lastVcTag,KK_ShareData.currentVCTag);
    
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(senderClick:) object:nil];
    [self performSelector:@selector(senderClick:) withObject:sender afterDelay:0.0];
    sender.backgroundColor = SNCOLORGRAY;
    sender.alpha = 0.4;
//    NSLog(@"sender tag>>>>>%d",sender.tag);
    self.selectedIndex = sender.tag;
    [UIView animateWithDuration:VIEWANIMATETIME animations:^{
        _lineView.frame = CGRectMake(HHHWIDTH/_vcs.count*sender.tag, 47 +FitSimpleIpx(0, 12), HHHWIDTH/_vcs.count, 2);
    }];
    
    for (UILabel *view in _tabBarView.subviews)
    {
        if ([view isKindOfClass:[UILabel class]])
        {
            if (view.tag == sender.tag + 100)
            {
                view.textColor = SNCOLORBLACK;
            }
            else
            {
                view.textColor = SNCOLORGRAY;
            }
        }
    }
    
    for (UIImageView *view in _tabBarView.subviews)
    {
        if ([view isKindOfClass:[UIImageView class]])
        {
            if (view.tag == sender.tag + 1000)
            {
                view.image = [UIImage image:_selectImageArray[view.tag - 1000]];
            }
            else
            {
                view.image = [UIImage image:_normalImageArray[view.tag - 1000]];
            }
            [view setTintColor:SNCOLORGRAY];
        }
    }
}
-(void)bleControlFindPhone{
    FindPhoneView *findPhoneView = [[FindPhoneView alloc]initWithFrame:CGRectMake(0, 0, HHHWIDTH, HHHHEIGHT)];
    [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:findPhoneView];
    [findPhoneView animationUp];
    if (IOS10_OR_LATER) {//ios 10
        
        UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
            
        UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
            
        content.title = [NSString localizedUserNotificationStringForKey:SN_Text(@"找手机") arguments:nil];
        
        content.sound = [UNNotificationSound defaultSound];
            
        //content.badge = [NSNumber numberWithInteger:1];
        
        NSCalendar *gregorian = [NSCalendar currentCalendar];
            
        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO];
        NSString *identifier = @"findPhone";
        UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
            
        [center addNotificationRequest:request withCompletionHandler:nil];

    }else{
    
        UILocalNotification *notification = [[UILocalNotification alloc] init];
        notification.timeZone = [NSTimeZone localTimeZone];
        notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];
        notification.repeatCalendar=[NSCalendar currentCalendar];
        notification.alertBody = [NSString localizedUserNotificationStringForKey:SN_Text(@"找手机") arguments:nil];
        notification.alertAction = @"打开应用";
        notification.soundName = UILocalNotificationDefaultSoundName;

        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }
}
-(void)showTheTabBarView
{
    [UIView animateWithDuration:0.2 animations:^{
        _tabBarView.frame = CGRectMake(0, HHHHEIGHT - 49 - FitSimpleIpx(0, 12), HHHWIDTH, 49);
    }];
    //    _tabBarView.hidden = NO;
}

-(void)hiddenTheTabBarView
{
    [UIView animateWithDuration:0.2 animations:^{
        _tabBarView.frame=CGRectMake(0, HHHHEIGHT, HHHWIDTH, 49);
    }];
    //    _tabBarView.hidden = YES;
}

@end
